﻿function dateAddExtention(p_Interval, p_Number) {
    var thing = new String();


    //in the spirt of VB we'll make this function non-case sensitive
    //and convert the charcters for the coder.
    p_Interval = p_Interval.toLowerCase();

    if (isNaN(p_Number)) {

        //Only accpets numbers 
        //throws an error so that the coder can see why he effed up    
        throw "The second parameter must be a number. \n You passed: " + p_Number;
        return false;
    }
    p_Number = new Number(p_Number);
    switch (p_Interval.toLowerCase()) {
        case "yyyy":
            {// year
                this.setFullYear(this.getFullYear() + p_Number);
                break;
            }
        case "q":
            {        // quarter
                this.setMonth(this.getMonth() + (p_Number * 3));
                break;
            }
        case "m":
            {        // month
                this.setMonth(this.getMonth() + p_Number);
                break;
            }
        case "y":        // day of year
        case "d":        // day
        case "w":
            {        // weekday
                this.setDate(this.getDate() + p_Number);
                break;
            }
        case "ww":
            {    // week of year
                this.setDate(this.getDate() + (p_Number * 7));
                break;
            }
        case "h":
            {        // hour
                this.setHours(this.getHours() + p_Number);
                break;
            }
        case "n":
            {        // minute
                this.setMinutes(this.getMinutes() + p_Number);
                break;
            }
        case "s":
            {        // second
                this.setSeconds(this.getSeconds() + p_Number);
                break;
            }
        case "ms":
            {        // second
                this.setMilliseconds(this.getMilliseconds() + p_Number);
                break;
            }
        default:
            {

                //throws an error so that the coder can see why he effed up and
                //a list of elegible letters.
                throw "The first parameter must be a string from this list: \n" +
                        "yyyy, q, m, y, d, w, ww, h, n, s, or ms. You passed: " + p_Interval;
                return false;
            }
    }
    return this;
}
Date.prototype.dateAdd = dateAddExtention;

function LastDayOfMonth(Year, Month) {
    return (new Date((new Date(Year, Month + 1, 1)) - 1)).getDate();
}

function setDatesFromInterval(selected, from, to) {
    var today = new Date();
    var month = today.getMonth();
    var day = today.getDay();
    var date = today.getDate();
    var year = today.getFullYear();

    var datefrom = '';
    var dateto = '';

    switch (selected) {
        case '1': //current week
            var startdate = today.dateAdd("d", -(day - 1));
            datefrom = (startdate.getDate() + '.' + (startdate.getMonth() + 1) + '.' + startdate.getFullYear());

            var enddate = startdate.dateAdd("d", 6);
            dateto = (enddate.getDate() + '.' + (enddate.getMonth() + 1) + '.' + enddate.getFullYear());
            break;
        case '2': //previous week
            var currentweekstartdate = today.dateAdd("d", -(day - 1));
            var startdate = currentweekstartdate.dateAdd("d", -7);
            datefrom = (startdate.getDate() + '.' + (startdate.getMonth() + 1) + '.' + startdate.getFullYear());

            var enddate = startdate.dateAdd("d", 6);
            dateto = (enddate.getDate() + '.' + (enddate.getMonth() + 1) + '.' + enddate.getFullYear());
            break;
        case '3': //current month
            datefrom = '01.' + (month + 1) + '.' + year;
            dateto = LastDayOfMonth(year, month) + '.' + (month + 1) + '.' + year;
            break;
        case '4': //previous month
            if (month == 0) {
                month = 11;
                year = year - 1;
            }
            else {
                month = month - 1;
            }
            datefrom = '01.' + (month + 1) + '.' + year;
            dateto = LastDayOfMonth(year, month) + '.' + (month + 1) + '.' + year;
            break;
        case '5': //current quartal
            var quartal = parseInt(month / 3);
            month = quartal * 3 + 1;
            datefrom = '01.' + month + '.' + year;

            month = month + 2;
            dateto = LastDayOfMonth(year, month) + '.' + month + '.' + year;
            break;
        case '6': //previous quartal
            var quartal = parseInt(month / 3);
            if (quartal == 0) {
                quartal = 3;
                year = year - 1;
            }
            else {
                quartal = quartal - 1;
            }
            month = quartal * 3 + 1;
            datefrom = '01.' + month + '.' + year;
            month = month + 2;
            dateto = LastDayOfMonth(year, month) + '.' + month + '.' + year;
            break;
        case '7': //current year
            datefrom = '01.01.' + year;
            dateto = '31.12.' + year;
            break;
        case '8': //previous year
            datefrom = '01.01.' + (year - 1);
            dateto = '31.12.' + (year - 1);
            break;
    }
    $("#" + from).val(datefrom);
    $("#" + to).val(dateto);
}

